package com.bgi.uims.module.sys.department.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bgi.uims.common.action.BaseAction;
import com.bgi.uims.common.dto.PageVo;
import com.bgi.uims.module.sys.department.dto.Department;
import com.bgi.uims.module.sys.department.service.DepartmentService;
import com.bgi.uims.module.sys.user.dto.User;
import com.bgi.uims.pub.utils.Constant;
import com.bgi.uims.pub.utils.PublicMethod;


/**
 * 
 * ClassName:DepartmentAction 
 * Function: TODO 
 * Reason:	 TODO 
 * @CreateDate:2015年3月23日 上午10:59:21
 * @author   WEN
 * @version  
 * @since    JDK 1.7
 * @see
 */
@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction{
	
	/**
	 * serialVersionUID:TODO(用一句话描述这个变量表示什么).
	 * @since JDK 1.7
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private DepartmentService departmentService;
	
	private String id;
	private PageVo pageVo = null;
	private Department department = null;
	private String q;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getQ() {
		return q;
	}
	public void setQ(String q) {
		this.q = q;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public PageVo getPageVo() {
		return pageVo;
	}

	public void setPageVo(PageVo pageVo) {
		this.pageVo = pageVo;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	/**
	 * 
	 * @Description：  加载组织信息
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:26:42
	 * @version: V1.0
	 */
	public String queryDepartment()
	{
		if (department == null){
			department = new Department();
		} else {
			if(department.getExcludeId() != null &&
					!"".equals(department.getExcludeId())){
				department.setExcludeIds(department.getExcludeId().split("\\,"));
			}
		}
		if (this.id != null){
			department.setParentId(id);
		}
		pageVo=new PageVo();
		department.getPage(this);
		pageVo=departmentService.queryDepartment(department);
		@SuppressWarnings("unchecked")
		List<Department> departments = pageVo.getRows();
		List<Department> root=new ArrayList<Department>();
		if(departments!=null&&departments.size()>0)
		{
			for(Department m:departments)
			{
				if("0".equals(m.getParentId().trim()))
				{
					m.setChildren(areaFilter(departments, m.getDepartmentId()));
					m.set_parentId(null);
					root.add(m);
				}
				//时间展示修改。
				if(m.getCreateTime() != null && m.getCreateTime().endsWith(".0")){
					m.setCreateTime(m.getCreateTime().replace(".0", ""));
				}
				if(m.getLastModifiedTime() != null && m.getLastModifiedTime().endsWith(".0")){
					m.setLastModifiedTime(m.getLastModifiedTime().replace(".0", ""));
				}
			}
		}
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT, Constant.DEPARTMENTMODULES,department, null));
		pageVo.setSuccess(true);
		pageVo.setRows(root);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  递归将数据的父子关系体现
	 * @author: WEN
	 * @CreateDate:2015年3月30日 下午3:32:47
	 * @version: V1.0
	 */
	public List<Department> areaFilter(List<Department> areas,String parentId){
		List<Department> trees = new ArrayList<Department>();
		for(Department p : areas){
			if(parentId.equals(p.getParentId())){
				p.setChildren(areaFilter(areas,p.getDepartmentId()));
				trees.add(p);
			}
		}
		return  trees;
	}
	
	/**
	 * 
	 * @Description：  通过条件查询组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 下午1:20:46
	 * @version: V1.0
	 */
	public String queryDepartmentAssociate()
	{
		if (this.id != null && !"".equals(this.id)){
			return this.queryDepartment();
		} else {
			pageVo=new PageVo();
			if(department == null){
				department = new Department();
			}
			List<Department> departments = new ArrayList<Department>();
			//先不分页
			//department.getPage(null);
			//上级查询
			pageVo=departmentService.queryDepartmentAssociate(department);
			@SuppressWarnings("unchecked")
			List<Department> upDepartments = pageVo.getRows();
			//下级查询
			pageVo=departmentService.queryLowerDepartmentAssociate(department);
			@SuppressWarnings("unchecked")
			List<Department> downDepartments = pageVo.getRows();
			Map<String,Department> mapDepartment = new HashMap<String,Department>();
			for(Department department:upDepartments){
				if(!mapDepartment.containsKey(department.getDepartmentId())){
					mapDepartment.put(department.getDepartmentId(), department);
					departments.add(department);
				}
			}
			for(Department department:downDepartments){
				if(!mapDepartment.containsKey(department.getDepartmentId())){
					mapDepartment.put(department.getDepartmentId(), department);
					departments.add(department);
				}
			}
			List<Department> root=new ArrayList<Department>();
			if(departments!=null&&departments.size()>0)
			{
				for(Department m:departments)
				{
					if("0".equals(m.getParentId().trim()))
					{
						m.setChildren(areaFilter(departments, m.getDepartmentId()));
						root.add(m);
						m.set_parentId(null);
					}
					//时间展示修改。
					if(m.getCreateTime() != null && m.getCreateTime().endsWith(".0")){
						m.setCreateTime(m.getCreateTime().replace(".0", ""));
					}
					if(m.getLastModifiedTime() != null && m.getLastModifiedTime().endsWith(".0")){
						m.setLastModifiedTime(m.getLastModifiedTime().replace(".0", ""));
					}
				}
			}
			session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT, Constant.DEPARTMENTMODULES,department, null));
			pageVo.setSuccess(true);
			pageVo.setRows(root);
			return SUCCESS;
		}
	}
	/**
	 * 
	 * @Description：  删除组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 下午1:21:00
	 * @version: V1.0
	 */
	public String deleteDepartment()
	{
		pageVo=new PageVo();
		String resultMsg = "";
		Department oledDepartMent = null;
		if(department.getDepartmentId() != null && 
				!"".equals(department.getDepartmentId())){
			String[] ids = department.getDepartmentId().split("\\,");
			oledDepartMent = departmentService.queryDepartmentById(department);
			department.setDepartmentIds(ids);
			
		}else {
			resultMsg = "没有选择删除项！";
			pageVo.setSuccess(false);
			pageVo.setMsg(resultMsg);
			return SUCCESS;
		}
		resultMsg = departmentService.deleteDepartment(department);
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.DELETE,Constant.DEPARTMENTMODULES , oledDepartMent, department));
		pageVo.setSuccess(true);
		pageVo.setMsg(resultMsg);
		return SUCCESS;
	}
	/**
	 * 
	 * @Description：  保存或更新组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 下午1:21:29
	 * @version: V1.0
	 */
	public String saveDepartment()
	{
		pageVo=new PageVo();
		//获得用户
		User sessionUser=(User) session.get(Constant.LOGINUSER);
		if(department.getDepartmentId() != null && !"".equals(department.getDepartmentId())){
			department.setLastModifiedUser(sessionUser.getRealName());
			Department oledDepartMent = departmentService.queryDepartmentById(department);
			Department parentDepartment = new Department();
			parentDepartment.setDepartmentId(oledDepartMent.getParentId());
			parentDepartment = departmentService.queryDepartmentById(parentDepartment);
			if(parentDepartment != null){
				oledDepartMent.setParentName(parentDepartment.getDepartmentName());
			} else {
				department.setParentName(null);
			}
			String resultMsg = departmentService.saveDepartment(department);
			if(resultMsg.equals("true")){
				session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.UPDATE, Constant.DEPARTMENTMODULES,oledDepartMent, department));
				pageVo.setSuccess(true);
				pageVo.setMsg(resultMsg);
			} else {
				pageVo.setSuccess(false);
				pageVo.setMsg(resultMsg);
			}
		} else {
			department.setCreateUser(sessionUser.getRealName());
			String resultMsg = departmentService.saveDepartment(department);
			if(resultMsg.equals("true")){
				session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.INSERT, Constant.DEPARTMENTMODULES ,department, null));
				pageVo.setSuccess(true);
				pageVo.setMsg(resultMsg);
			} else {
				pageVo.setSuccess(false);
				pageVo.setMsg(resultMsg);
			}
		}
		
		return SUCCESS;
	}
	/**
	 * 
	 * @Description： 更改组织状态 
	 * @author: WEN
	 * @CreateDate:2015年3月23日 下午1:21:11
	 * @version: V1.0
	 */
	public String updateDepartmentStatus()
	{
		pageVo=new PageVo();
		User sessionUser=(User) session.get(Constant.LOGINUSER);
		department.setLastModifiedUser(sessionUser.getRealName());
		Department databaseDepartMent = departmentService.queryDepartmentById(department);
		Department oldDepartMent = new Department();
		oldDepartMent.setDepartmentId(databaseDepartMent.getDepartmentId());
		oldDepartMent.setDepartmentState(databaseDepartMent.getDepartmentState());
		String resultMsg = departmentService.updateDepartmentStatus(department);
		if("".equals(resultMsg)){
			pageVo.setSuccess(true);
			session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.UPDATE, Constant.DEPARTMENTMODULES,oldDepartMent, department));
			pageVo.setMsg(resultMsg);
		} else {
			pageVo.setSuccess(false);
			pageVo.setMsg(resultMsg);
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description： 获得组织的下级组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 下午1:21:11
	 * @version: V1.0
	 */
	public String queryLowerDepartmentAssociate()
	{
		Department resultDepartment = new Department();
		pageVo=new PageVo();
		pageVo = departmentService.queryLowerDepartmentAssociate(department);
		@SuppressWarnings("unchecked")
		List<Department> results = pageVo.getRows();
		String departmentNames = "";
		String[] departmentIds = new String[results.size()];
		int i = 0;
		for(Department department:results){
			departmentNames += department.getDepartmentName() + ";  <br/>";
			departmentIds[i++] = department.getDepartmentId();
		}
		resultDepartment.setDepartmentName(departmentNames);
		resultDepartment.setDepartmentIds(departmentIds);
		pageVo.setObject(resultDepartment);
		pageVo.setSuccess(true);
		pageVo.setMsg(departmentNames);
		return SUCCESS;
	}
	/**
	 * @Description：  查询单个组织信息
	 * @author: tantianhua
	 * @CreateDate:2015年4月2日 下午3:19:27
	 * @version: V1.0
	 */
	public String queryDepartmentById()
	{
		pageVo=new PageVo();
		pageVo.setObject(departmentService.queryDepartmentNameById(department));
		pageVo.setSuccess(true);
		return SUCCESS;
	}
}